package com.file.service.impl;

import com.file.service.FileDownService;
import com.file.utiltools.error.ToolsException;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Mars-Hasee on 2017/7/30.
 */
@Service
public class FileDownServiceImpl implements FileDownService {
    @Override
    public Integer getCount() throws ToolsException {
        return null;
    }

    @Override
    public Map<String,String> getFileName() throws ToolsException {
       Map<String,String> map = new HashMap<>();
        // get file list where the path has
        String path = "J://电影种子";
//        String path = "D://迅雷下载";
//        D:\迅雷下载
        File file = new File(path);
        // get the folder list
        File[] array = file.listFiles();
        for(int i=0;i<array.length;i++){
            if(array[i].isFile()){
                map.put(array[i].getName(),array[i].getPath());
//                // only take file name
//                System.out.println("^^^^^" + array[i].getName());
//                // take file path and name
////                System.out.println("#####" + array[i]);
////                // take file path and name
////                System.out.println("*****" + array[i].getPath());
            }else if(array[i].isDirectory()){
              String newpath=array[i].getPath();
                Map<String,String> map2= this.getFile(newpath);
                map.putAll(map2);
            }
        }
        return map;
    }

   public   Map<String,String>  getFile(String path){
       Map<String,String> map = new HashMap<>();
       File file = new File(path);
       // get the folder list
       File[] array = file.listFiles();
       for(int i=0;i<array.length;i++){
           if(array[i].isFile()){
               map.put(array[i].getName(),array[i].getPath());
//                // only take file name
//                System.out.println("^^^^^" + array[i].getName());
//                // take file path and name
////                System.out.println("#####" + array[i]);
////                // take file path and name
////                System.out.println("*****" + array[i].getPath());
           }else if(array[i].isDirectory()){
               String newpath=array[i].getPath();
               Map<String,String> map2 =   this.getFile(newpath);
               map.putAll(map2);
           }
       }
       return map;
   }



}
